﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Office.Interop;
using Word = Microsoft.Office.Interop.Word;
using Excel = Microsoft.Office.Interop.Excel;

namespace DocsCheck
{
    public partial class MainForm : Form
    {
        ArrayList m_XlsFiles;
        ArrayList m_DocFiles;
        ArrayList m_KeyList = new ArrayList();

        public void ListFiles(string rootFolder, ref ArrayList DocFiles, ref ArrayList XlsFiles)
        {
            DirectoryInfo TheFolder = new DirectoryInfo(rootFolder);

            DirectoryInfo[] dirInfo = TheFolder.GetDirectories();


            //遍历文件夹
            foreach (DirectoryInfo NextFolder in dirInfo)
            {
                FileInfo[] fileInfo = NextFolder.GetFiles();
                //遍历文件
                foreach (FileInfo NextFile in fileInfo)
                {
                    if (NextFile.Name.StartsWith("~"))
                        continue;

                    if (NextFile.Name.Contains("doc"))
                        DocFiles.Add(NextFile.FullName);

                    if (NextFile.Name.Contains("xls"))
                        XlsFiles.Add(NextFile.FullName);
                }
            }


        }

        public void ListFilesEx(string rootFolder, ref ArrayList DocFiles, ref ArrayList XlsFiles)
        {
            DirectoryInfo TheFolder = new DirectoryInfo(rootFolder);

            FileInfo[] fileInfo = TheFolder.GetFiles();
            //遍历文件
            foreach (FileInfo NextFile in fileInfo)
            {
                if (NextFile.Name.StartsWith("~"))
                    continue;

                if (NextFile.Name.Contains("doc"))
                    DocFiles.Add(NextFile.FullName);

                if (NextFile.Name.Contains("xls"))
                    XlsFiles.Add(NextFile.FullName);
            }

            DirectoryInfo[] dirInfo = TheFolder.GetDirectories();
            //遍历文件夹
            foreach (DirectoryInfo NextFolder in dirInfo)
            {
                ListFilesEx(NextFolder.FullName, ref DocFiles, ref XlsFiles);
            }
        }
        
        public MainForm()
        {
            InitializeComponent();


            m_KeyList.Add("大庆");
            m_KeyList.Add("昌邑");
            m_KeyList.Add("华星");
            m_KeyList.Add("正和");
            m_KeyList.Add("聚甲醛");
            m_KeyList.Add("上海");
            m_KeyList.Add("济南");
            m_KeyList.Add("安迪苏");
            m_KeyList.Add("中原");
            m_KeyList.Add("沧州");
            m_KeyList.Add("南通");
            m_KeyList.Add("实施数据库");
            m_KeyList.Add("实时项目");
        }

        private void DocsCheck_Click(object sender, EventArgs e)
        {
            object SaveChanges = false; //保存更改
            object OriginalFormat = System.Type.Missing;
            object RouteDocument = System.Type.Missing;

            textBoxOutput.Clear();

            string RootFolder;
            FolderBrowserDialog FolderDlg = new FolderBrowserDialog();
            FolderDlg.SelectedPath = @"D:\svn_workspace\shdoc";
            if (FolderDlg.ShowDialog() == DialogResult.OK)
            {
                RootFolder = FolderDlg.SelectedPath;
            }
            else
                return;



            m_DocFiles = new ArrayList();
            m_XlsFiles = new ArrayList();

            ListFilesEx(RootFolder, ref m_DocFiles, ref m_XlsFiles);

            object nullobj = System.Reflection.Missing.Value;



            try
            {
                Word.Application appWord = new Word.Application();
                appWord.Visible = false;

                foreach (string filename in m_DocFiles)
                {
                    //object FileName = @"E:\test.docx";
                    textBoxOutput.AppendText(filename + "\n");
                    Word.Document doc = appWord.Documents.Open(filename);
                    string textString = doc.Content.Text.Trim();

                    foreach (string key in m_KeyList)
                    {
                        if (textString.Contains(key))
                        {
                            textBoxOutput.AppendText("find：" + key + "\n");
                        }
                    }


                    //关闭文件
                    doc.Close(ref SaveChanges, ref OriginalFormat, ref RouteDocument);
                }

                appWord.Quit();

                /*
                Excel.Application appExcel = new Excel.Application(); 
                foreach (string filename in m_XlsFiles)
                {
                    textBoxOutput.AppendText(filename + "\n");
                    Excel.Workbook workbook = appExcel.Workbooks.Open(filename);
                    foreach (Excel.Worksheet worksheet in workbook.Worksheets)
                    {
                        textBoxOutput.AppendText("worksheet：" + worksheet.Name + "\n");
                        
                        int RowNum = worksheet.Rows.Count;
                        int ColNum = worksheet.Columns.Count;

                        for (int i = 0; i < RowNum; i++)
                        {
                            for (int j = 0; j < ColNum; j++)
                            {
                                
                               
                            }
                        }
                        

                    }
                    workbook.Close(); 
                }
                   
                appExcel.Quit();
*/
            }
            catch (Exception ex)
            {
                textBoxOutput.AppendText("出现错误：" + ex.ToString() + "\n");
            }
            textBoxOutput.AppendText("完成" + "\n");
        }

    }
}
